<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>11.数组移动函数实例</title>
</head>
<body>
  <script>
    function move(array,from,to){
      if(from<0|| to>=array.length){
        console.error("参数错误");
        return;
      }
      //创建新数组，因为数组是引用类型修改后会改变原数组，扩展类型是浅拷贝，修改
      const newArray = [...array];
      let item = newArray.splice(from,1);//返回值是一个数组,相当于把from位置的元素移出来
      newArray.splice(to, 0, ...item);// 不用扩展符号就会向newArray添加数组，相当于把刚刚移出来的元素添加到to的位置上去
      return newArray;
    }
    let arr = [1,2,3,4];
   console.table(move(arr,1,3));
  </script>
</body>
</html>